<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28
 * Time: 11:32
 */

namespace app\admin\controller;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;


class Order extends Common
{

    /**
     * 订单列表数据接口
     * @return \think\response\Json
     */
    public function orderList()
    {
        $post = Request::instance()->post();
        $where = [];
        $sortName = $post['sortName'];
        $sortOrder = $post['sortOrder'];
        if (0 == $post['pageNumber']){
            $page = 1;
        }else{
            $page = ($post['pageNumber']/10)+1;
        }
        $limit = $post['pageSize'] ? $post['pageSize'] : 10;
        $limit1= ($page - 1 ) * $limit . ',' . $limit;
        if(array_key_exists('order_no' , $post['search']) && !empty($post['search']['order_no']))
            $where['o.order_no'] = ['like' , '%' . $post['search']['order_no'] . '%'];
        if(array_key_exists('pro_name' , $post['search']) && !empty($post['search']['pro_name']))
            $where['p.name'] = ['like' , '%' . $post['search']['pro_name'] . '%'];
        if(array_key_exists('phone' , $post['search']) && !empty($post['search']['phone']))
            $where['u.phone'] = ['like' , '%' . $post['search']['phone'] . '%'];
        if(array_key_exists('status' , $post['search']) && !empty($post['search']['status']))
            $where['o.status'] = ['EQ' ,  $post['search']['status']];
        if(array_key_exists('add_time' , $post['search']) && !empty($post['search']['add_time']))
            $where['o.add_time'] = ['between' , [$post['search']['add_time'].' 00:00:00' , $post['search']['add_time'].' 23:59:59']];
        if(array_key_exists('addr' , $post['search']) && !empty($post['search']['addr']))
            $where['u.country|u.city|u.town'] = ['like' , '%' . $post['search']['addr'] . '%'];
        if(array_key_exists('status' , $post['search']) && !empty($post['search']['status']))
            $where['o.status'] = ['IN' , $post['search']['status']];
        $info = Db::name('order')
            ->alias('o')
            ->field('o.id,o.order_no,s.name,u.user_name,add_time,o.money,o.status')
            ->join('order_info oi','o.id = oi.order_id')
            ->join('product p' , 'oi.product_id = p.id')
            ->join('supplier s' , 'o.supplier_id= s.id')
            ->join('user u' , 'u.id= o.user_id')
            ->where($where)
//            ->order('s.name desc,u.user_name')
            ->order("$sortName $sortOrder")
            ->group('o.id')
            ->limit($limit1)
            ->select();
        $count = Db::name('order')
            ->alias('o')
            ->field('o.id,o.order_no,s.name,u.name as user_name,o.money,o.status')
            ->join('order_info oi','o.id = oi.order_id')
            ->join('product p' , 'oi.product_id = p.id')
            ->join('supplier s' , 'o.supplier_id= s.id')
            ->join('user u' , 'u.id= o.user_id')
            ->where($where)
            ->group('o.id')
            ->count();
        if($info)
        {
            foreach ($info as $key => $value)
            {
                $info[$key]['money'] =  sprintf("%.2f", $value['money']) . Session::get('flag');
                if($value['status'] == 1)
                {
                    $info[$key]['status'] = '待处理';
                }
                else if($value['status'] == 2)
                {
                    $info[$key]['status'] = '待发货';
                }
                else if($value['status'] == 3)
                {
                    $info[$key]['status'] = '待确认';
                }
                else if($value['status'] == 4)
                {
                    $info[$key]['status'] = '待评价';
                }
                else if($value['status'] == 5)
                {
                    $info[$key]['status'] = '用户已评价';
                }
                else if($value['status'] == 7)
                {
                    $info[$key]['status'] = '商户已评价';
                }
                else if($value['status'] == 6)
                {
                    $info[$key]['status'] = '已取消';
                }
            }
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        }
        else
        {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }

    /**
     * 订单详情
     */
    public function getOrderInfo()
    {
        $id = input('post.id');
        if($id)
        {
            $info = Db::name('order')
                ->alias('o')
                ->field('o.id,u.name,u.user_name,o.addr,u.email,u.phone,u.addr,s.name as sup_name,s.manager,s.manager_tel,s.address,s.email as sup_email,o.money,o.send_time')
                ->join('supplier s' , 'o.supplier_id= s.id')
                ->join('user u' , 'u.id= o.user_id')
                ->where('o.id',$id)
                ->find();
            $info['comment'] = Db::name('comment')
                ->where('order_id',$info['id'])
                ->where('comment_type',1)
                ->value('comment');
            if($info)
            {
                $order_info = Db::name('order_info')
                    ->field('order_id,product_id,specification_name,num,price,packing_proportion')
                    ->where('order_id' , $info['id'])
                    ->select();
                foreach ($order_info as $key => $value)
                {
                    $order_info[$key]['price'] = Session::get('flag') . sprintf("%.2f", $value['price']);
                    $value['total_price'] = $value['num'] * $value['price'];
                    $pro_info = Db::name('product')
                        ->alias('p')
                        ->field('p.name as pro_name,p.origin,p.is_include_tax,bd.name as type')
                        ->where('p.id',$value['product_id'])
                        ->join('base_data bd' , 'bd.id = p.category_id')
                        ->find();
                    $comment_info = Db::name('comment')
                        ->field('comment')
                        ->where([
                            'order_id' => $value['order_id'],
                            'product_id' => $value['product_id'],
                            'comment_type' => 0,
                        ])
                        ->find();
                    if($comment_info)
                    {
                        $order_info[$key] = array_merge($pro_info , $comment_info , $value);
                    }
                    else
                    {
                        $order_info[$key] = array_merge($pro_info , $value);
                    }
                }
                $info['pro_info'] = $order_info;
                foreach ($info['pro_info'] as $key => $value)
                {
                    if($value['is_include_tax'] == 0)
                    {
                        $info['pro_info'][$key]['is_include_tax'] = '否';
                    }
                    else
                    {
                        $info['pro_info'][$key]['is_include_tax'] = '是';
                    }
                }
                $result = Config::get('code.success');
                $result['data'] = $info;
            }
            else
            {
                $result = Config::get('code.null');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 修改订单状态
     */
    public function updateOrderStatus()
    {
        $data = Request::instance()->post();
        if(array_key_exists('id' , $data) && array_key_exists('status' , $data))
        {
            $status = Db::name('order')->where('id',$data['id'])->update(['status' => $data['status']]);
            if($status)
            {
                $result = Config::get('code.success');
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }
}